﻿using UnityEngine;
using System.Collections;

public class CameraControllerAlpha : MonoBehaviour {

	public bool debugMode = false;
	public string playername = "Player";
	public Camera playerCamera;
	public float camSpeed = 0.1f;
	Vector3 newPos = new Vector3(0,0,0);

	void Start(){
		gameObject.transform.renderer.material.color = Color.red;
	}

	void FixedUpdate(){
	}

	void Update(){
		moveCamera();
	}

	void OnTriggerEnter(Collider collider) {
		//is Collider a Player?
		if(collider.gameObject.name == playername){

			if(debugMode){
				Debug.Log(playername + " entered " + gameObject.name);
				gameObject.transform.renderer.material.color = Color.red;
			}
			string direction = GetDirection(collider.gameObject);
			Debug.Log (direction);
			if(direction.Equals("top")){
				//newPos = playerCamera.transform.position + new Vector3(0,0,500);
				playerCamera.transform.Translate(transform.up * 10f);
			}
			if(direction.Equals("bottom")){
				//newPos = playerCamera.transform.position + new Vector3(0,0,-500);
				playerCamera.transform.Translate(-transform.up * 10f);
			}
			if(direction.Equals("left")){
				//newPos = playerCamera.transform.position + new Vector3(-500,0,0);
				playerCamera.transform.Translate(-transform.right * 10f);
			}
			if(direction.Equals("right")){
				//newPos = playerCamera.transform.position + new Vector3(500,0,0);
				playerCamera.transform.Translate(transform.right * 10f);
			}
			//implement Function to pause Activity in a tile
		}
	}

	void OnTriggerStay(Collider collider) {
		if(collider.gameObject.name == playername){
			if(debugMode){
				Debug.Log(playername + " is staying in " + gameObject.name);
				gameObject.transform.renderer.material.color = Color.green;
			}
		}
	}

	void OnTriggerExit(Collider collider) {
		if(collider.gameObject.name == playername){
			if(debugMode){
				Debug.Log(playername + " left " + gameObject.name);
				gameObject.transform.renderer.material.color = Color.red;
			}
			//playerCamera.camera.fieldOfView = 110;
			//implement Function to start Activity in a tile
		}
	}

	string GetDirection(GameObject obj){
		Vector2 vector = new Vector2(
			gameObject.transform.position.x - obj.transform.position.x,
			gameObject.transform.position.z - obj.transform.position.z
		);
		Vector2 normalized = (1/vector.magnitude) * vector;
		string direction = "x: " + normalized.x + " y: " + normalized.y;
		if(normalized.y < 0 && normalized.x < 0.6f && normalized.x > (-0.6f)){
			direction = "bottom";
		}
		else if(normalized.y > 0 && normalized.x < 0.6f && normalized.x > (-0.6f)){
			direction = "top";
		}
		else if(normalized.x < 0 && normalized.y < 0.6f && normalized.y > (-0.6f)){
			direction = "left";
		}
		else if(normalized.x > 0 && normalized.y < 0.6f && normalized.y > (-0.6f)){
			direction = "right";
		}
		return direction;
	}

	void moveCamera(){
		Vector3 oldPos = playerCamera.transform.position;
		if(newPos != ((new Vector3(0,0,0)))){
			playerCamera.transform.position = Vector3.Lerp(oldPos,newPos,Time.deltaTime * camSpeed);
			newPos = new Vector3(0,0,0);
		}
	}

}
